package com.example.vehicle.Function;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.SM2;

import java.security.KeyPair;

/**
 * @name: SM2Test
 * @description: 测试用SM2加解密类。
 * @author: Lulu Zhao
 **/
public class SM2Test {

    public static KeyPair pair = SecureUtil.generateKeyPair("SM2");
    public static String publicKey = Base64.encode(pair.getPublic().getEncoded());
    public static String privateKey = Base64.encode(pair.getPrivate().getEncoded());

    /**
     * @name: main
     * @description: 主函数。
     * @params: [args]
     * @return: void
     * @author: Lulu Zhao
     */
    public static void main(String[] args) {
        String text = "test demo";
        SM2 sm2 = new SM2(null, publicKey);
        String encryptStr = sm2.encryptBcd(text, KeyType.PublicKey);
        String encryptStr1 = sm2.encryptBcd(text, KeyType.PublicKey);

        SM2 sm21 = new SM2(privateKey, null);
        String decryptStr = StrUtil.utf8Str(sm21.decryptFromBcd(encryptStr, KeyType.PrivateKey));
        String decryptStr1 = StrUtil.utf8Str(sm21.decryptFromBcd(encryptStr, KeyType.PrivateKey));
        System.out.println(encryptStr);
        System.out.println(encryptStr1);
        System.out.println(decryptStr);
        System.out.println(decryptStr1);
    }
}
